package khs.java_async_callback.main.java;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws InterruptedException, ExecutionException
    {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<String> fut1 = executor.submit(new CallableImpl("async1"));
        Future<String> fut2 = executor.submit(new CallableImpl("async2"));
        
        for(int i=0;i<100;i++){
            System.out.println("main: " + i);
        }
        
        String callback1 = fut1.get();
        String callback2 = fut2.get();
        
        System.out.println(callback1);
        System.out.println(callback2);
    }
}

final class CallableImpl implements Callable<String>
{
    private String arg0;
    
    public CallableImpl(String arg0){
        this.arg0 = arg0;
    }
    
    public String call() throws Exception {
        // TODO Auto-generated method stub
        for(int i=0;i<100;i++){
            System.out.println(arg0 + ": " + i);
        }
        
        return this.arg0 + " callback";
    }
}
